package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.MemberMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.date.DateUtils;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.h2.mvstore.DataUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Service
@Transactional
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService{
    @Reference
    OrderService orderService;

    @Override
    @LcnTransaction
    public int saveMember(Member member) {
        save(member);
        return member.getId();
    }

    @Override
    @LcnTransaction
    public Member findMemberByEmail(String email) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        return getOne(queryWrapper);
    }

    @Override
    public Map getMemberCount() {
        Map map = new HashMap();
        List<Map> mapList = baseMapper.getMemberCount();
        List<Long> memberCounts = new ArrayList<>();
        List<String> yearAndMonth = new ArrayList<>();
        for (Map m : mapList) {
            memberCounts.add((Long) m.get("memberCounts"));
            yearAndMonth.add((String) m.get("yearAndMonth"));
        }
        map.put("memberCounts",memberCounts);
        map.put("yearAndMonth",yearAndMonth);
        return map;
    }

    @SneakyThrows
    @Override
    public Map getDataCount() {
        Map map = new HashMap();
        Date date = new Date();
        //总会员数
        int totalMember = count();
        //今日新增会员
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("regtime", DateUtils.parseDate2String(date));
        int todayNewMember = count(queryWrapper);
        //本周新增会员数
        QueryWrapper<Member> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.between("regtime",DateUtils.parseDate2String(DateUtils.getFirstDayOfWeek(date)),DateUtils.parseDate2String(DateUtils.getLastDayOfWeek(date)));
        int thisWeekNewMember = count(queryWrapper1);
        //本月新增会员数
        QueryWrapper<Member> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.between("regtime", DateUtils.parseDate2String(DateUtils.getFirstDayDateOfMonth(date)),DateUtils.parseDate2String(DateUtils.getLastDayOfMonth(date)));
        int thisMonthNewMember = count(queryWrapper2);

        Map mapOrders = orderService.getOrderData();

        List<Map> mapList = orderService.getHotSetmeal();

        map.put("totalMember",totalMember);
        map.put("todayNewMember",todayNewMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",mapOrders.get("todayOrderNumber"));
        map.put("todayVisitsNumber",mapOrders.get("todayVisitsNumber"));
        map.put("thisWeekOrderNumber",mapOrders.get("thisWeekOrderNumber"));
        map.put("thisWeekVisitsNumber",mapOrders.get("thisWeekVisitsNumber"));
        map.put("thisMonthOrderNumber",mapOrders.get("thisMonthOrderNumber"));
        map.put("thisMonthVisitsNumber",mapOrders.get("thisMonthVisitsNumber"));
        map.put("hotSetmeal",mapList);

        return map;
    }
}
